/************************************************************************************
* FileName                                                                          *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "SimpleCollectionView.h"
#include "collector/CollectorItem.h"
#include "collector/Collector.h"
#include "collector_common/SimpleCollectionSortFilter.h"

#include <QMenu>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QContextMenuEvent>
#include <QModelIndexList>
#include <QModelIndex>
#include <QVariant>
#include <QHeaderView>
#include <QHBoxLayout>
#include <QLabel>

SimpleCollectionView::SimpleCollectionView(
           PartialCollectionModel *model,
           SimpleCollectionSortFilter *filter,
           QWidget *parent,
           bool showFilter)
: QWidget(parent), m_model(model),
  m_view(new QTreeView(this)),
  m_showFilter(showFilter),
  m_proxyModel(filter),
  m_filter(new SearchBox(this)),
  m_contextActions(NULL)
{
    m_proxyModel->setParent(this);
    m_proxyModel->setDynamicSortFilter(true);
    setupUi();
    setupActions();
    setupConnections();
}


SimpleCollectionView::~SimpleCollectionView() {
//    delete m_model;
    delete m_proxyModel;
    delete m_view;
    delete m_filter;
    if(m_contextActions != NULL) {
        for(int i = 0; i < m_contextActions->size(); ++i) {
            delete m_contextActions->at(i);
        }
        delete m_contextActions;
    }
}


void SimpleCollectionView::setupUi() {
    this->setAutoFillBackground(true);
    m_proxyModel->setSourceModel(m_model);
    m_view->setModel(m_proxyModel);
    m_view->setAlternatingRowColors(true);
    m_view->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_view->setSortingEnabled(true);
    m_view->setWordWrap(false);
    m_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
    m_view->setFocusPolicy(Qt::WheelFocus);
    m_view->setAutoScroll(true);
    m_view->header()->setResizeMode(QHeaderView::Stretch);
    m_view->hideColumn(0);
    m_view->verticalScrollBar()->setAutoFillBackground(true);
    m_view->setFont(QFont(m_view->font().defaultFamily(), 9));
    m_view->setEditTriggers(QAbstractItemView::CurrentChanged);
    m_view->setObjectName("ywidget");
    m_view->header()->setObjectName("ywidget");

    QVBoxLayout *layout = new QVBoxLayout();
    QHBoxLayout *miniLayout = new QHBoxLayout();
    miniLayout->addWidget(new QLabel("Filter"));
    miniLayout->addWidget(m_filter);
    layout->addLayout(miniLayout);
    layout->addWidget(m_view);
    this->setLayout(layout);
    this->setObjectName("yampcomp");
    if(!m_showFilter) {
        m_filter->hide();
    }
}

void SimpleCollectionView::setupConnections() {
    connect(m_view, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(tableDoubleClicked()));
    connect(m_filter, SIGNAL(textChanged(QString)), m_proxyModel, SLOT(setExpression(QString)));
}

void SimpleCollectionView::setupActions() {
    //Setup the tool bar actions.
}


void SimpleCollectionView::tableDoubleClicked() {
    QModelIndexList indices = m_view->selectionModel()->selectedRows(0);
    if(indices.empty()) {
        return;
    }
    CollectorItem *item = Collector::get()->item(indices.at(0).data().toString());
    emit itemActivated(item);
}


void SimpleCollectionView::contextMenuEvent(QContextMenuEvent *event) {
    emit contextMenuRequested(); //This is the time when you can disable/enable actions
    if(m_contextActions != NULL) {
        QMenu menu(this);
        for(int i = 0; i < m_contextActions->size(); i++) {
            menu.addAction(m_contextActions->at(i));
        }
        menu.exec(event->globalPos());
    }
}


void SimpleCollectionView::setContextActions(QList<QAction *> *actions) {
    if(m_contextActions == NULL) {
        m_contextActions = actions;
    }
    else {
        m_contextActions->append(*actions);
        delete actions;
    }
}

PartialCollectionModel *SimpleCollectionView::model() {
    return m_model;
}

QList<QString> *SimpleCollectionView::selectedTrackIds() {
    QList<QString> *trackIds = new QList<QString>();
    QModelIndexList indexList = m_view->selectionModel()->selectedRows(0);
    for(int i = 0; i < indexList.size(); ++i) {
        trackIds->append(indexList.at(i).data().toString());
    }
    return trackIds;
}


void SimpleCollectionView::setSortingEnabled(bool value) {
    m_view->setSortingEnabled(value);
}


void SimpleCollectionView::sort(int column, Qt::SortOrder order) {
    m_proxyModel->sort(column, order);
}


void SimpleCollectionView::setFilterExpression(QString regEx) {
    m_proxyModel->setExpression(regEx);
}
